<?php

class ReviewsController extends GxController {

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id, 'Reviews'),
        ));
    }

    public function actioncreate() {
        $this->layout=false;
        $model = new Reviews;
        if (isset($_POST['text'])) {
            Yii::app()->session['class_course_review'] = $_POST;
            $userid = getLoginUserId();
            if (!empty($userid)) {
                if (isset($_POST['classId'])) {                    
                    $model->setAttribute('classes_id', $_POST['classId']);
                    $classCourseUsers = Classes::model()->findByPk($_POST['classId']);                  
                    $classCourseUser = ($classCourseUsers->users_id != '') ? $classCourseUsers->users_id : '';
                    $reviewsUser = $model->findByAttributes(array('classes_id' => $_POST['classId'], 'users_id' => $userid));
                } if (isset($_POST['courseId'])) {
                    $model->setAttribute('courses_id', $_POST['courseId']);
                    $classCourseUser = Courses::model()->findByPk($_POST['courseId']);
                    $classCourseUser = ($classCourseUser->users_id != '') ? $classCourseUser->users_id : '';
                    $reviewsUser = $model->findByAttributes(array('courses_id' => $_POST['courseId'], 'users_id' => $userid));
                }

                if ($reviewsUser <> "" && $classCourseUser != $userid && $reviewsUser->users_id != $userid) {
                    $model->setAttribute('reviews_text', $_POST['text']);
                    $model->setAttribute('users_id', $userid);
                    $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
                    $model->setAttribute('create_time', SkillConfig::CreateDateTime());
                    if ($model->save(FALSE)) {
                        echo Message::REVIEWS_SUCCESS_MSG;
                        unset(Yii::app()->session['class_course_review']);
                    }
                } elseif ($reviewsUser == "" && $classCourseUser != $userid) {
                    $model->setAttribute('reviews_text', $_POST['text']);
                    $model->setAttribute('users_id', $userid);
                    $model->setAttribute('status', SkillConfig::STATUS_ACTIVE);
                    $model->setAttribute('create_time', SkillConfig::CreateDateTime());
                    if ($model->save(FALSE)) {
                        echo Message::REVIEWS_SUCCESS_MSG;
                        unset(Yii::app()->session['trainer_profile_review']);
                    }
                }

                if ($reviewsUser <> "" && ($reviewsUser->users_id == $userid)) {
                    echo 'againsameuser';
                    unset(Yii::app()->session['trainer_profile_review']);
                }

                if ($classCourseUser == $userid) {
                    echo 'fail';
                    unset(Yii::app()->session['trainer_profile_review']);
                }
            } else {
                echo 'redirect';
            }
        }
    }

    public function actiontestimonials() {
        //when need to show class review than replace  TrainerProfileReview to Reviews
        
        $dataProvider = new CActiveDataProvider('TrainerProfileReview', array(
            'criteria' => array(
                'condition' => 'status=' . SkillConfig::STATUS_ACTIVE,
                'order' => 'create_time DESC',
            ),
            'pagination' => array(
                'pageSize' => 10,
            ),
        ));
        $this->render('_viewAllReview', array('dataProvider' => $dataProvider));
    }

    public function actionuserreviews() {
        $reviewMessage = '';
        if (isset($_GET['skillcode'])) {
            if ($_GET['skillcode'] == Message::success) {
                $reviewMessage = Message::REVIEWS_SUCCESS_MSG;
            }
            if ($_GET['skillcode'] == Message::repeat_user) {
                $reviewMessage = 'You  have already made a query for this class.';
            }
            if ($_GET['skillcode'] == Message::owan_user) {
                $reviewMessage = "You can't give own review.";
            }
        }
        if (isset($_GET['workshop']) && isset($_GET['type'])) {
            $classCourseId = (isset($_GET['type']) && $_GET['type'] == SkillConfig::class_name) ? 'classes_id' : 'courses_id';
            $reviewdata = Reviews::model()->findAllByAttributes(array($classCourseId => $_GET['workshop'], 'status' => SkillConfig::STATUS_ACTIVE));
            $this->render('_userreviews', array('reviewdata' => $reviewdata, 'reviewMessage' => $reviewMessage));
        } else {
            throw new CHttpException(403, 'You are not authorized to perform this action.');
        }
    }

}
